<?php
/**
 * Checkout page controller
 *
 * @author Dima
 *
 */

class CheckoutController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = false;

	public $ajaxable = array(
		'checkdelivery' => array('json')
	);

//----------------------------------------------------------------------------------------------------

	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			parent::init();
			$this->view->title = 'Checkout';

			$this->view->headLink()->appendStylesheet('/css/validation_css/screen.css');
			$this->view->headScript()->appendFile('/js/jquery.validate.js');
			$this->view->headScript()->appendFile('/js/checkout.js');
		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
		if (0 == Lemar_Shop::getItemsInCart())
		{
			$this->_redirect('/cart/');
		}
		$values = array(
			'address' => '',
			'city' => '',
			'state' => '',
			'notes' => '',
			'email' => '',
			'phone' => '',
			'apt' => '',
		);
		if (Lemar_User::isLogged())
		{
			$user = Lemar_User::getLoggedUser();
			$values['address'] = $user->address;
			$values['city'] = $user->city;
			$values['state'] = $user->state;
			$values['notes'] = '';
			$values['email'] = $user->email;
			$values['phone'] = $user->phone;
			$values['apt'] = $user->apt;
		}

		$request = $this->getRequest();

		if ($request->isPost())
		{
			$form = new forms_OrderForm();

			if ($form->isValid($request->getPost()))
			{
				$values['address'] = $form->getValue('address');
				$values['city'] = $form->getValue('city');
				$values['state'] = $form->getValue('state');
				$values['notes'] = $form->getValue('notes');
				$values['email'] = $form->getValue('email');
				$values['phone'] = $form->getValue('phone');
				$values['apt'] = $form->getValue('apt');
				$coupon = $form->getValue('coupon');
				if ('' != $coupon)
				{
					if (!Lemar_Shop::checkCoupon($coupon, Lemar_User::getLoggedUserId()))
					{
						$error = 'Wrong coupon code';
					}
					else
					{
						$couponCode = models_CouponcodesMapper::findByCode($coupon);
						$values['coupon_code_id'] = $couponCode->id;
					}
				}
				if (!isset($error))
				{

					$displayValue = Lemar_Shop::checkout($values);
					$this->_redirect('/checkout/thankyou/?total='.$displayValue);
				}
			}
			else
			{
				$error = 'Please fill in right values';
			}
		}

		$this->view->values = $values;
		if (isset($error))
		{
			$this->view->error = $error;
		}

		$this->view->totalAmount = Lemar_Shop::getTotalAmount();

		$allStates 	  = models_CountriesMapper::getAllStates();
		$this->view->allStates = $allStates;
	}

//----------------------------------------------------------------------------------------------------

	public function checkdeliveryAction()
	{
		$street = $this->_getParam('address');
		$city = $this->_getParam('city');
		$state = $this->_getParam('state');

		$this->view->result = models_Google::isInArea($street.', '.$city.', '.$state); //street, city, state
	}

//----------------------------------------------------------------------------------------------------

	public function thankyouAction()
	{
		$displayValue = floatval($this->_getParam('total', 0));
		$this->view->total = $displayValue;
	}

//----------------------------------------------------------------------------------------------------

}
